package com.mi.aop;
import javax.servlet.http.HttpServletRequest;

import com.mi.util.DataUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Arrays;
import java.util.Date;

@Aspect
@Component
public class WebLogAspect {

    private final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

    @Pointcut("execution(public * com.mi.service.impl..*.*(..))")//切入点描述 这个是controller包的切入点
    public void serviceLog(){}//签名，可以理解成这个切入点的一个名称

    @Before("serviceLog()") //在切入点的方法run之前要干的
    public void logBeforeController(JoinPoint joinPoint) {

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();//这个RequestContextHolder是Springmvc提供来获得请求的东西
        logger.info("【mi】"+ DataUtil.getData() +"service 携带参数: " + Arrays.toString(joinPoint.getArgs()));
        logger.info("【mi】"+ DataUtil.getData() +"service 方法开始执行 : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

    }
    @After("serviceLog()") //在切入点的方法run之前要干的
    public void logAfterController(JoinPoint joinPoint) {
        Date date = new Date();
        // 记录下请求内容
        logger.info("【mi】"+ DataUtil.getData() +"service 方法执行完毕 : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
    }

}